﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;
using System.IO;
using System.Text.RegularExpressions;

namespace DTcms.Web.m
{
    /// <summary>
    /// get_message_list 的摘要说明
    /// </summary>
    public class get_message_list : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string result = DTRequest.GetString("result");

            BLL.users bll = new BLL.users();
            Regex reg = new Regex("\"targetId\":\"([0-9]+)\"", RegexOptions.IgnoreCase);
            MatchCollection mac = reg.Matches(result.ToString());
            foreach (Match m in mac)
            {
                string str = m.Groups[0].Value;
                int _target_id = int.Parse(m.Groups[1].ToString());
                Model.users model = bll.GetModel(_target_id);
                if (model != null)
                {
                    string _username = model.user_name;
                    string _avatar = model.avatar;
                    if (string.IsNullOrEmpty(_avatar))
                    {
                        _avatar = "../image/tx.jpg";
                    }
                    else if (_avatar.Contains("shicaiquanzi"))
                    {
                        _avatar = "http://" + _avatar;
                    }
                    else
                    {
                        _avatar = "http://www.shicaiquanzi.com" + _avatar;
                    }

                    result = result.Replace(str, str + ",\"userName\":\"" + _username + "\",\"userAvatar\":\"" + _avatar + "\"");
                }
            }
            context.Response.Write(result.ToString());
            return;
        }
       

        public static void WriteTxt(string text, string filepath = "")
        {
            if (string.IsNullOrEmpty(filepath))
                filepath = HttpContext.Current.Request.MapPath("/abc.txt");
            using (var fs = new FileStream(filepath, FileMode.Append, FileAccess.Write))
            {
                using (var write = new StreamWriter(fs))
                {
                    write.WriteLine(text);
                    write.Flush();
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}